package com.advertisementsystem.Controller;

import com.advertisementsystem.Dto.PermissionIdsRequest;
import com.advertisementsystem.Dto.RoleDTO;
import com.advertisementsystem.Exception.ResponseResult;
import com.advertisementsystem.Servie.RoleService;
import com.advertisementsystem.entity.Permission;
import com.advertisementsystem.entity.Role;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/role")
@Validated
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 角色列表（分页）
     */
    @GetMapping("/list")
    public ResponseResult<Page<Role>> getRoleList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String code,
            @RequestParam(required = false) Integer status) {
        return ResponseResult.success(roleService.getRoleList(pageNum, pageSize, name, code, status));
    }

    /**
     * 全部角色
     */
    @GetMapping("/all")
    public ResponseResult<List<Role>> getAllRoles() {
        return ResponseResult.success(roleService.getAllRoles());
    }

    /**
     * 获取角色详情
     */
    @GetMapping("/{id}")
    public ResponseResult<Role> getRoleById(@PathVariable Long id) {
        return ResponseResult.success(roleService.getRoleById(id));
    }

    /**
     * 增加角色
     */
    @PostMapping
    public ResponseResult<?> addRole(@RequestBody @Valid RoleDTO roleDTO) {
        roleService.addRole(roleDTO);
        return ResponseResult.success("");
    }

    /**
     * 更新角色
     */
    @PutMapping("/{id}")
    public ResponseResult<?> updateRole(@PathVariable Long id,
                                        @RequestBody @Valid RoleDTO roleDTO) {
        roleService.updateRole(id, roleDTO);
        return ResponseResult.success("");
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/{id}")
    public ResponseResult<?> deleteRole(@PathVariable Long id) {
        roleService.deleteRole(id);
        return ResponseResult.success("");
    }

    /**
     * 更新角色状态
     */
    @PutMapping("/{id}/status")
    public ResponseResult<?> updateRoleStatus(@PathVariable Long id, @RequestParam Integer status) {
        roleService.updateRoleStatus(id, status);
        return ResponseResult.success("");
    }

    /**
     * 为角色分配权限
     */
    @PostMapping("/{roleId}/permissions")
    public ResponseResult<?> assignPermissions(
            @PathVariable Long roleId,
            @RequestBody PermissionIdsRequest request
    ) {
        roleService.assignPermissions(roleId, request.getPermissionIds());
        return ResponseResult.success("");
    }

    /**
     * 获取角色的权限列表
     */
    @GetMapping("/{roleId}/permissions")
    public ResponseResult<List<Permission>> getRolePermissions(@PathVariable Long roleId) {
        return ResponseResult.success(roleService.getRolePermissions(roleId));
    }
}